# 键盘控制rect方向, 按一次动一次
import pygame
import sys

pygame.init()

winx = 300
winy = 300
screen = pygame.display.set_mode((winx, winy))
BLACK = 0, 0, 0
WHITE = 255, 255, 255
r1_x = 100
r1_y = 100
speed = 5
fclock = pygame.time.Clock()

while True:
    screen.fill(BLACK)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
        # 按下键盘

        elif event.type == pygame.KEYDOWN:
            print('keydown')
            if event.key == pygame.K_UP:
                r1_y -= speed
                print('up')
            elif event.key == pygame.K_DOWN:
                r1_y += speed
            elif event.key == pygame.K_LEFT:
                r1_x -= speed
            elif event.key == pygame.K_RIGHT:
                r1_x += speed

    r1 = r1_x, r1_y, 50, 50
    pygame.draw.rect(screen, WHITE, r1)
    pygame.display.update()
    fclock.tick(20)
